package com.qfedu.food.service.impl;

import com.qfedu.food.service.intf.RedisService;
import com.qfedu.food.util.LikedStatusEnum;
import com.qfedu.food.util.RedisKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.Map;

/**
 * @program: RedisServiceImpl
 * @description:
 * @author: Feri(段超帅)
 * @create: 2021/1/12  20:28
 */

public class RedisServiceImpl implements RedisService {
    @Autowired
    RedisTemplate redisTemplate;

//    @Autowired
//    LikedService likedService;

    @Override
    public void saveLiked2Redis(String likedUserId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, likedPostId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikedStatusEnum.LIKE.getCode());
    }

    @Override
    public void unlikeFromRedis(String likedUserId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, likedPostId);
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_USER_LIKED, key, LikedStatusEnum.UNLIKE.getCode());
    }

    @Override
    public void deleteLikedFromRedis(String likedUserId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedUserId, likedPostId);
        redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
    }

    @Override
    public void incrementLikedCount(String likedUserId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, likedUserId, 1);
    }

    @Override
    public void decrementLikedCount(String likedUserId) {
        redisTemplate.opsForHash().increment(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, likedUserId, -1);
    }

//    @Override
//    public List<UserLike> getLikedDataFromRedis() {
//        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED, ScanOptions.NONE);
//        List<UserLike> list = new ArrayList<>();
//        while (cursor.hasNext()){
//            Map.Entry<Object, Object> entry = cursor.next();
//            String key = (String) entry.getKey();
//            //分离出 likedUserId，likedPostId
//            String[] split = key.split("::");
//            String likedUserId = split[0];
//            String likedPostId = split[1];
//            Integer value = (Integer) entry.getValue();
//
//            //组装成 UserLike 对象
//            UserLike userLike = new UserLike(likedUserId, likedPostId, value);
//            list.add(userLike);
//
//            //存到 list 后从 Redis 中删除
//            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED, key);
//        }
//
//        return list;
//    }
//
//    @Override
//    public List<LikedCountDTO> getLikedCountFromRedis() {
//        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, ScanOptions.NONE);
//        List<LikedCountDTO> list = new ArrayList<>();
//        while (cursor.hasNext()){
//            Map.Entry<Object, Object> map = cursor.next();
//            //将点赞数量存储在 LikedCountDT
//            String key = (String)map.getKey();
//            LikedCountDTO dto = new LikedCountDTO(key, (Integer) map.getValue());
//            list.add(dto);
//            //从Redis中删除这条记录
//            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_USER_LIKED_COUNT, key);
//        }
//        return list;
//    }
}
